<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>02-eventbus实现非父子组件通讯</title>
  </head>
  <body>
    <div id="app">
      <h1>vue的例子</h1>
      <p>{{msg}}</p>
      <my-foo></my-foo>
      <my-demo></my-demo>
    </div>
    <script src="./js/vue.js"></script>
    <script>
      Vue.config.productionTip = false // 关闭生产提示
      const bus = new Vue()
      Vue.component('my-foo', {
        template: `
            <div>
                <p>foo组件</p>
                <p>{{price}}</p>
            </div>
        `,
        data() {
          return {
            price: '',
          }
        },
        mounted() {
          bus.$on('getPrice', (money) => {
            this.price = money
          })
        },
      })
      Vue.component('my-demo', {
        template: `
        <div>
          <button @click="send">发送price</button>
        </div>
      `,
        data() {
          return {
            price: 20000,
          }
        },
        methods: {
          send() {
            bus.$emit("getPrice",this.price)
          },
        },
      })
      const vm = new Vue({
        el: '#app',
        data: {
          msg: 'hello',
        },
      })
    </script>
  </body>
</html>
